import re, os
from website.website_base import WebSiteBase, FetchQueueItem, WebSiteDb
from bs4 import BeautifulSoup
from lib.log import DBG, INFO, ERROR, EXCEPTION
from lib.sqlite_ext import DBApiExt
from website.homeweb.homeweb import Homeweb


class Xcd(Homeweb):
    """
    """
    INDEX_URL = "https://www.xcd4.autos"
    cmdpath = os.path.abspath(os.getcwd())
    DB = os.path.join(cmdpath, "website", "xcd", "xcd.db")
    SAVE_DIR = os.path.join(cmdpath, "website", "xcd", "page")

    def __init__(self):
        super().__init__()

    @classmethod
    def _class_config(cls):
        """
        获取所有分类首页url列表

        :return:
        """

        class_list = [
            {"name": "熟母少妇", "id": "20"},
            {"name": "网红直播", "id": "21"},
            {"name": "自拍偷拍", "id": "22"},
            {"name": "强奸乱伦", "id": "23"},
            {"name": "高清国产", "id": "24"},
            {"name": "韩国专区", "id": "25"},
            {"name": "日本有码", "id": "26"},
            {"name": "日本无码", "id": "27"},
            {"name": "欧美情色", "id": "28"},
            {"name": "动漫卡通", "id": "29"},
            {"name": "三级伦理", "id": "30"}
        ]

        return class_list

    def _parse_class_page_a_list_content(self, soup):
        li_div = soup.select_one(".watchlist")
        lis = li_div.find_all("li")
        return lis

    def for_class_page_a_list(self, a_content, class_id):
        a = a_content.find("a")
        link_hover = a.attrs["href"]
        titlep = a_content.select_one(".title")
        title = titlep.attrs["title"]
        title = title.replace("\"", "")
        title = title.replace("\'", "")
        img = a.find("img")
        head_img_src = img.attrs["data-original"]
        res_id = self._get_res_id_from_href(link_hover)
        if res_id is None:
            return None
        else:
            record = {
                "id": res_id,
                "class_id": class_id,
                "name": title,
                "res_type": WebSiteDb.RES_TYPE_VIDEO,
                "head_image": head_img_src,
                "url": link_hover,
                "parse": 0
            }
            return record
